﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TERP.WaterClient.Common;
using DevExpress.XtraEditors;

namespace TERP.WaterClient.Views
{
    public partial class FrmLoading : XtraForm, ILoadingListener
    {
        private FrmLogin _frmLogin;
        private bool _loadSuccess = false;

        public FrmLoading(FrmLogin frmLogin)
        {
            InitializeComponent();
            this._frmLogin = frmLogin;
            this.InitControl();
        }

        public void InitControl(){
            this.lblProgress.Appearance.Font = LayoutStyle.FONT_BOLD;
            this.progressBar.Properties.Minimum = 0;
            this.progressBar.Properties.Maximum = 100;
            this.Text = "Loading";
        }

        private void bgrWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            MasterInit masterInit = MasterInit.Instance;
            masterInit.SetListener(this);
            try
            {
                masterInit.StartLoading();
                _loadSuccess = true;
            }
            catch (Exception ex)
            {
                _loadSuccess = false;
				Util.ShowExceptionBox(ex);
                return;
            }
        }

        private void bgrWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.lblProgress.Text = (e.ProgressPercentage.ToString() + "%");
            this.progressBar.Position = e.ProgressPercentage;
        }

        private void bgrWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           
            this.Close();
            _frmLogin.LoadFinish(_loadSuccess);
        }

        private void FrmLoading_Load(object sender, EventArgs e)
        {
            if (bgrWorker.IsBusy != true)
            {
                bgrWorker.RunWorkerAsync();
            }
        }

        #region ILoadingListener Members

        public void Loading_Progress(int percent)
        {
            bgrWorker.ReportProgress(percent);
        }

        public void Loading_Completed()
        {
            //*throw new NotImplementedException();*/
        }

        #endregion
    }
}
